package com.imay.live.adapter;

import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by jeaman on 2017/4/14.
 */

public class ChooseTagAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private final static int MAX_CHOOSE = 3;
    private Set<String> mChoseSet = new HashSet<>();

    public ChooseTagAdapter(List<String> data) {
        super(R.layout.item_choose_tag, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        TextView nameTv = helper.getView(R.id.choose_tag_tv_name);
        nameTv.setText(mContext.getString(R.string.choose_tag_format, item));
        if (mChoseSet.contains(item)) {
            helper.setVisible(R.id.choose_tag_iv_check, true);
            nameTv.setTextColor(mContext.getResources().getColor(R.color.text_75));
        } else {
            helper.setVisible(R.id.choose_tag_iv_check, false);
            nameTv.setTextColor(mContext.getResources().getColor(R.color.text_21));
        }
    }

    public void removeChooseTag(String tag) {
        mChoseSet.remove(tag);
        notifyDataSetChanged();
    }

    //切换选中状态，如果存在返回true表示移除当前tag
    public boolean toggleChooseTag(String tag) throws Exception {
        boolean remove = false;
        if (mChoseSet.contains(tag)) {
            mChoseSet.remove(tag);
            remove = true;
        } else if (mChoseSet.size() >= MAX_CHOOSE) {
            throw new Exception();
        } else{
            mChoseSet.add(tag);
        }
        notifyDataSetChanged();
        return remove;
    }
}
